package cn.mcch.investigator.adapter.base;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;

import java.util.List;


/**
 * Created by Administrator on 2016/12/20.
 */
public abstract class CommonExpandableListAdapter<K,T> extends BaseExpandableListAdapter {

    private Context context;
    private List<K> groupDatas;
    private List<List<T>> childDatas;
    private int groupLayoutId;
    private int childLayoutId;

    public CommonExpandableListAdapter(Context context, List<K> groupDatas, List<List<T>> childDatas, int groupLayoutId, int childLayoutId) {
        this.context = context;
        this.groupDatas = groupDatas;
        this.childDatas = childDatas;
        this.groupLayoutId = groupLayoutId;
        this.childLayoutId = childLayoutId;
    }

    @Override
    public int getGroupCount() {
        return groupDatas.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return childDatas.get(groupPosition).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groupDatas.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return childDatas.get(groupPosition).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

        ViewHolder viewHolder = ViewHolder.get(context, convertView, parent, groupLayoutId, groupPosition);
        bindGroupData(viewHolder, groupPosition, groupDatas.get(groupPosition));
        return viewHolder.getConvertView();
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = ViewHolder.get(context, convertView, parent, childLayoutId, groupPosition);
        bindChildData(viewHolder, groupPosition, childPosition, childDatas.get(groupPosition).get(childPosition));
        return viewHolder.getConvertView();
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    protected abstract void bindGroupData(ViewHolder viewHolder, int groupPosition, K data);

    protected abstract void bindChildData(ViewHolder viewHolder, int groupPosition, int childPosition, T data);
}
